﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gunningine
{
    internal class Quadtree
    {
        public QuadtreeNode RootNode { get; private set; }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="items">items to hold in quadtree.</param>
        public Quadtree(IQuadtreeItem[] items, int maxDepth = 8, int maxItems = 16)
        {
            RootNode = new QuadtreeNode(items, 0, maxDepth, maxItems);
        }

        /// <summary>
        /// Perform frustum-culling on quadtree, using the specified camera,
        /// and return visible items.
        /// </summary>
        /// <param name="camera"></param>
        /// <returns></returns>
        public List<IQuadtreeItem> GetItemsInFrustum(RenderSystem renderSystem, Camera camera)
        {
            List<IQuadtreeItem> items = new List<IQuadtreeItem>();

            Plane[] planes = renderSystem.GetClippingPlanes(camera);
            this.RootNode.GetItemsInClippingPlanes(planes, items);

            return items;
        }

        /// <summary>
        /// Render
        /// </summary>
        /// <param name="renderSystem"></param>
        public void Render(RenderSystem renderSystem)
        {
            renderSystem.SetColour(0, 1, 0);

        }
    }
}
